package net.zjitc.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/9/21.
 */

public class AServlet implements Servlet {
    //第一步：定义成员变量servletConfig,所有方法都可以使用
    private ServletConfig servletConfig = null;
    /**
     * 默认第一次访问
     * @param servletConfig
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("猴王出世");
        this.servletConfig = servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * 每次有请求，都有此方法处理
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        PrintWriter out = servletResponse.getWriter();
        String value = this.servletConfig.getInitParameter("Ecoding");
        out.write("aServlet....1");
        ServletContext context = this.servletConfig.getServletContext();
        context.setAttribute("name","zhangsan");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("die");
    }
}
